<template>
  <el-main>
    <!-- 搜索框 -->
    <el-form
      ref="searchForm"
      :model="parms"
      label-width="80px"
      :inline="true"
      size="small"
    >
      <el-form-item label="姓名">
        <el-input v-model="parms.userName" />
      </el-form-item>
      <el-form-item label="房屋编号">
        <el-input v-model="parms.houseNum" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button style="color: #ff7670" icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button v-if="hasPerm('sys:feeWater:add')" type="primary" icon="el-icon-plus" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableList" :height="tableHeight" border stripe>
      <el-table-column label="姓名" prop="loginName" />
      <el-table-column label="电话" prop="phone" />
      <el-table-column label="栋数" prop="name" />
      <el-table-column label="单元" prop="unitName" />
      <el-table-column label="房屋编号" prop="houseNum" />
      <el-table-column label="缴费金额" prop="payWaterMoney" />
      <el-table-column label="所属月份" prop="payWaterMonth" />
      <el-table-column label="缴费状态" prop="payWaterStatus">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.payWaterStatus == '0'"
            type="danger"
            size="small"
          >未缴费</el-tag>
          <el-tag
            v-if="scope.row.payWaterStatus == '1'"
            type="success"
            size="small"
          >已缴费</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="270" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="hasPerm('sys:feeWater:edit')"
            icon="el-icon-edit"
            type="primary"
            size="small"
            @click="editBtn(scope.row)"
          >编辑</el-button>
          <el-button
            v-if="hasPerm('sys:feeWater:delete')"
            icon="el-icon-delete"
            type="danger"
            size="small"
            @click="deleteBtn(scope.row)"
          >删除</el-button>
          <el-button
            v-if="scope.row.payWaterStatus == '0' && hasPerm('sys:feeWater:pay')"
            icon="el-icon-delete"
            type="warning"
            size="small"
            @click="payBtn(scope.row)"
          >缴费</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page.sync="parms.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="parms.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="parms.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />
    <!-- 新增弹框 -->
    <sys-dialog
      :title="addDialog.title"
      :height="addDialog.height"
      :width="addDialog.width"
      :visible="addDialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <template slot="content">
        <el-form
          ref="addForm"
          :model="addModel"
          :rules="rules"
          label-width="80px"
          :inline="true"
          size="small"
        >
          <el-form-item prop="buildId" label="栋数">
            <el-select v-model="addModel.buildId" @change="selectBuild">
              <el-option
                v-for="item in buildList"
                :key="item.buildId"
                :label="item.name"
                :value="item.buildId"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="unitId" label="单元">
            <el-select v-model="addModel.unitId" @change="selectUnit">
              <el-option
                v-for="item in unitList"
                :key="item.unitId"
                :label="item.unitName"
                :value="item.unitId"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="houseId" label="房屋编号">
            <el-select v-model="addModel.houseId">
              <el-option
                v-for="item in houseList"
                :key="item.houseId"
                :label="item.houseNum"
                :value="item.houseId"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="payWaterMonth" label="所属月份">
            <el-date-picker
              v-model="addModel.payWaterMonth"
              format="yyyy-MM"
              value-format="yyyy-MM"
              type="month"
              placeholder="选择月份"
            />
          </el-form-item>
          <el-form-item prop="payWaterMoney" label="缴费金额">
            <el-input v-model="addModel.payWaterMoney" />
          </el-form-item>
          <el-form-item style="margin-left:15px;" prop="waterNum" label="表显">
            <el-input v-model="addModel.waterNum" />
          </el-form-item>
          <el-form-item prop="payWaterStatus" label="缴费状态">
            <el-radio-group v-model="addModel.payWaterStatus">
              <el-radio :label="'0'">未缴费</el-radio>
              <el-radio :label="'1'">已缴费</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </template>
    </sys-dialog>
  </el-main>
</template>

<script>
import { getBuildApi, getUnitListByIdApi } from '@/api/houseList'
import { getListApi, getHouseByUnitIdApi, addWaterApi, editWaterApi, deleteWaterApi, payWaterApi } from '@/api/feeWater'
import SysDialog from '@/components/system/SysDialog'
export default {
  // 组件注册
  components: {
    SysDialog
  },
  data() {
    return {
      // 房屋编号列表
      houseList: [],
      // 单元列表
      unitList: [],
      // 栋数列表
      buildList: [],
      // 表单验证规则
      rules: {
        buildId: [
          {
            trigger: 'blur',
            required: true,
            message: '请选择栋数'
          }
        ],
        unitId: [
          {
            trigger: 'blur',
            required: true,
            message: '请选择单元'
          }
        ],
        houseId: [
          {
            trigger: 'blur',
            required: true,
            message: '请选择房屋编号'
          }
        ],
        payWaterMonth: [
          {
            trigger: 'blur',
            required: true,
            message: '请选择所属月份'
          }
        ],
        payWaterMoney: [
          {
            trigger: 'blur',
            required: true,
            message: '请填写缴费金额'
          }
        ],
        waterNum: [
          {
            trigger: 'blur',
            required: true,
            message: '请填写表显'
          }
        ],
        payWaterStatus: [
          {
            trigger: 'blur',
            required: true,
            message: '请选择缴费状态'
          }
        ]
      },
      // 新增弹框绑定数据域
      addModel: {
        editType: '',
        waterId: '',
        buildId: '',
        unitId: '',
        houseId: '',
        payWaterMonth: '',
        payWaterMoney: '',
        waterNum: '',
        payWaterStatus: ''
      },
      // 弹框属性
      addDialog: {
        title: '',
        height: 250,
        width: 650,
        visible: false
      },
      // 表格高度
      tableHeight: 0,
      // 表格列表
      tableList: [],
      parms: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        userName: '',
        houseNum: ''
      }
    }
  },
  created() {
    this.getList()
    this.getBuildList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 210
    })
  },
  methods: {
    // 单元点击事件
    async selectUnit(val) {
      this.addModel.houseId = ''
      this.getHouseByUnitId(val)
    },
    // 根据栋数id查询单元列表
    async getUnitListById(val) {
      const res = await getUnitListByIdApi({ buildId: val })
      console.log('单元')
      console.log(res)
      if (res && res.code === 200) {
        this.unitList = res.extend.list
      }
    },
    // 栋数点击事件
    selectBuild(val) {
      this.addModel.unitId = ''
      this.addModel.houseId = ''
      console.log(val)
      this.getUnitListById(val)
    },
    // 获取栋数列表
    async getBuildList() {
      const res = await getBuildApi()
      if (res && res.code === 200) {
        // 把栋数赋值到列表
        this.buildList = res.extend.list
      }
    },
    async getHouseByUnitId(val) {
      const res = await getHouseByUnitIdApi({ unitId: val })
      console.log(res)
      if (res && res.code === 200) {
        this.houseList = res.extend.list
      }
    },
    // 弹框确认事件
    onConfirm() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          let res = null
          if (this.addModel.editType === '0') {
            res = await addWaterApi(this.addModel)
          } else {
            res = await editWaterApi(this.addModel)
          }
          if (res && res.code === 200) {
            // 刷新列表
            this.getList()
            // 信息提示
            this.$message.success(res.msg)
            this.addDialog.visible = false
          }
        }
      })
    },
    // 弹框关闭事件
    onClose() {
      this.addDialog.visible = false
    },
    // 页数改变时触发
    currentChange(val) {
      this.parms.currentPage = val
      this.getList()
    },
    // 页容量改变时触发
    sizeChange(val) {
      this.parms.pageSize = val
      this.getList()
    },
    // 缴费按钮
    async payBtn(row) {
      console.log(row)
      // 提示信息
      const confirm = await this.$myconfirm('确定缴费吗？')
      if (confirm) {
        const parm = {
          waterId: row.waterId,
          payWaterStatus: '1'
        }
        const res = await payWaterApi(parm)
        if (res && res.code === 200) {
          // 刷新列表
          this.getList()
          // 信息提示
          this.$message.success(res.msg)
        }
      }
    },
    // 删除按钮
    async deleteBtn(row) {
      const confirm = await this.$myconfirm('确定删除该数据吗？')
      if (confirm) {
        const res = await deleteWaterApi({ waterId: row.waterId })
        if (res && res.code === 200) {
          // 刷新表格
          this.getList()
          this.$message.success(res.msg)
        }
      }
    },
    // 编辑按钮
    editBtn(row) {
      console.log(row)
      // 查询单元列表
      this.getUnitListById(row.buildId)
      // 查询房屋列表
      this.getHouseByUnitId(row.unitId)
      // 清空表单
      this.$resetForm('addForm', this.addModel)
      // 设置编辑属性
      this.addModel.editType = '1'
      // 设置弹框属性
      this.addDialog.visible = true
      this.addDialog.title = '编辑水费'
      // 把当前要编辑的数据设置到表单数据域
      this.$objCoppy(row, this.addModel)
    },
    // 新增按钮
    addBtn() {
      // 清空表单
      this.$resetForm('addForm', this.addModel)
      // 设置编辑属性
      this.addModel.editType = '0'
      // 设置弹框属性
      this.addDialog.visible = true
      this.addDialog.title = '新增水费'
    },
    // 重置按钮
    resetBtn() {
      // 把搜索框数据域清空
      this.parms.userName = ''
      this.parms.houseNum = ''
      // 刷新列表
      this.getList()
    },
    // 搜索按钮
    searchBtn() {
      // 刷新列表
      this.getList()
    },
    // 获取表格列表
    async getList() {
      const res = await getListApi(this.parms)
      console.log(res)
      if (res && res.code === 200) {
        // 把返回的值赋值到列表数据域
        this.tableList = res.extend.list.records
        this.parms.total = res.extend.list.total
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
